package Server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.GridBagLayout;

import javax.swing.JComboBox;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import Client.Client;
import Client.clienteGUI;
import Client.procesaVariable;
import clasesBasicas.Accion;
import clasesBasicas.Variable;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class FrameAccionServer extends JFrame {

	private JPanel contentPane;


	/**
	 * Create the frame.
	 */
	public FrameAccionServer(String id_placa,String id_variable) {
//		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JComboBox comboBox = new JComboBox();


		ArrayList <String> list= new ArrayList <String>();
		try {
			list=consultasBD.consultaAcciones(id_placa,id_variable);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		int i=0;
		String temp;
		ArrayList <Accion> listaacciones= new ArrayList <Accion>();
		for(i=0;i<list.size();i++){
			procesaVariable rec=new procesaVariable(list.get(i));
			Accion atemp=new Accion();
			int cod_var;
			String accion;
			int cod_accion;
			cod_var= Integer.parseInt(rec.getVar(0).substring(5));
			accion= rec.getVar(1);
			cod_accion=Integer.parseInt(rec.getVar(2));
			atemp.setCod_variable(cod_var);
			atemp.setAccion(accion);
			atemp.setCod_accion(cod_accion);
			listaacciones.add(atemp);
			temp=list.get(i);
		}
		
		
		String acciones[]= new String [listaacciones.size()];
		for(int t=0;t<listaacciones.size();t++)
		{
			acciones[t]=listaacciones.get(t).getAccion();
		}
		
		//CREA COMBO BOX
//		comboBox.setModel(new DefaultComboBoxModel(new String[] {"texto1", "texto2"}));
		comboBox.setModel(new DefaultComboBoxModel(acciones));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.gridwidth = 4;
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 0;
		gbc_comboBox.gridy = 1;
		panel.add(comboBox, gbc_comboBox);
		
		JButton btnSeleccionarAccin = new JButton("Seleccionar Acci\u00F3n");
		btnSeleccionarAccin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
//				String id_placa=id_placa;
//				String id_variable=id_variable;
				String id_accion=acciones[comboBox.getSelectedIndex()];
				String temp_id_placa=id_placa;
				String temp_id_variable=id_variable;
				String temp_id_accion = id_accion;
				try {
					String estadoplaca=consultasBD.consultaEstadoPlaca(id_placa);
					if(estadoplaca.equals("ON"))
					{
						String estadovariable=consultasBD.consultaEstadoVariable(id_variable);
						if(estadovariable.equals("ON"))
						{
							String seleccion = JOptionPane.showInputDialog(	btnSeleccionarAccin,  "Introduzca el temporizador",JOptionPane.QUESTION_MESSAGE);  // el icono sera un iterrogante

							if(seleccion!=null)
							{
								String cadena3="CONFIRMAR_ACCION "+seleccion;
								int tempo=Integer.parseInt(seleccion);
								String respuesta="409 ERR";
								if(tempo>=0)
								{
									consultasBD.consultaSetUltimaAccion(temp_id_variable, temp_id_placa, temp_id_accion);
									respuesta = "206 OK";

								}
				
								
								if(respuesta.startsWith("206"))
								{
									//todo correcto , cerrar y confirmar
									JOptionPane.showMessageDialog(null,"OK Accion sobre el sensor confirmada.");
//									clienteGUI.window.table.setValueAt(acciones[comboBox.getSelectedIndex()], clienteGUI.window.ysel, 5);
									dispose();
								}
								else if(respuesta.startsWith("409"))
								{
									//error , cerrar
									JOptionPane.showMessageDialog(null,"ERROR Faltan datos.");
									dispose();
								}

								//							respuesta=Client.consulta("CONFIRMAR_ACCION "+temporizador).get(0);
							}
							else if(seleccion==null)
							{
							}
							
						}else{
						}
					}else{
					}
				
			
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		GridBagConstraints gbc_btnSeleccionarAccin = new GridBagConstraints();
		gbc_btnSeleccionarAccin.insets = new Insets(0, 0, 0, 5);
		gbc_btnSeleccionarAccin.gridx = 1;
		gbc_btnSeleccionarAccin.gridy = 4;
		panel.add(btnSeleccionarAccin, gbc_btnSeleccionarAccin);
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addMouseListener(new MouseAdapter() {
			@Override
			
			public void mouseReleased(MouseEvent e) {
				dispose();
			}
		});
		GridBagConstraints gbc_btnVolver = new GridBagConstraints();
		gbc_btnVolver.gridx = 3;
		gbc_btnVolver.gridy = 4;
		panel.add(btnVolver, gbc_btnVolver);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.NORTH);
		
		JLabel lblSeleccionarAccin = new JLabel("Seleccionar acci\u00F3n");
		panel_1.add(lblSeleccionarAccin);
	}

}
